/**
 * Focus window class.
 * Windows that receives focus can handle the onblur and onfocus events.
 * Alsow all keybord events are available.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see InputWindow
 */

import core.ui.Window;
import core.util.Timer;

class FocusWindow extends Window {

	/**
	 * Creates new focus window.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] window size.
	 */
	this.FocusWindow = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.setTabIndex(-1);	// Set default tab index
	}
	
	/**
	 * Sets whether or not the window should gain focus.
	 *
	 * @param focus (bool) true if the window should gain focus, false othwerise.
	 */
	this.setFocus = function(focus) {
		if (focus) {
			if (this.getElement().focus) {
				// The timeout is necessary in both IE and Firefox 1.5,
				// to prevent scripts from doing strange unexpected things
				// as the user clicks on buttons and other controls.
				var element = this.getElement();
				var timer = new Timer(0);
				timer.addTimerListener({
					onTimer : function() {
						element.focus();
					}
				});
				timer.start();
			}
		} else {
			if (this.getElement().blur) {
				this.getElement().blur();
			}
		}
	}
	
	/**
	 * Gets whether the window has focus (not implemented).
	 *
	 * @return bool
	 */
	this.hasFocus = function() {
		// TODO: implement
	}
	
	/**
	 * Sets the window tab index.
	 *
	 * @param index (int) tab index.
	 */
	this.setTabIndex = function(index) {
		this.getElement().tabIndex = index;
	}
	
	/**
	 * Gets the window tab index.
	 *
	 * @return int
	 */
	this.getTabIndex = function() {
		return this.getElement().tabIndex;
	}
	
	/**
	 * Sets the window access key.
	 *
	 * @param k (char) acces key.
	 */
	this.setAccessKey = function(k) {
		this.getElement().accessKey = k;
	}
	
	/**
	 * Gets the window access key.
	 *
	 * @return char
	 */
	this.getAccessKey = function() {
		return this.getElement().accessKey;
	}
}